﻿/*=======================================================================
 * 
 * FILE: Bird.cs
 * AUTH: Alex
 * 
 * DESC: A bird-type enemy
 * 
 * NOTE: I fixed the general behavior of the bird enemy
 * but it only moves in one direction (right-to-left) (Matthew)
 * 
 *=======================================================================*/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class Bird : Enemy
    {
        // Movement properties.
        float radius;
        float angle; // in degrees
        float velocity;
        float center;
        
        /// <summary>
        /// The direction this enemy is facing and moving along the X axis.
        /// </summary>
        private FaceDirection direction = FaceDirection.Left;

        /// <summary>
        /// How long to wait before turning around.
        /// </summary>
        private const float MaxWaitTime = 1.0f;
        
        // Sprite path ...
        const string SPRITE_SET = "Bird";
        
        /// <summary>
        /// Constructs a new Enemy.
        /// </summary>
        public Bird(Level newLevel, Vector2 newPosition, ContentManager newContent)
            : base(newLevel, newPosition, SPRITE_SET, newContent)
        {
            damage = 40;
            angle = 0;
            radius = 20f;
            velocity = -0.5f;
            center = position.Y;

            // Set values for HP and points
            base.hp = 30;
            base.points = 100;
        }

        // The bird moves according to a sine curve.
        private void move()
        {
            angle += 5f;
            radius += 1.5f;
            radius = MathHelper.Clamp(radius, 0, 50);

            position = new Vector2(position.X + velocity, (radius * (float)Math.Cos(MathHelper.ToRadians(angle))) + center);

            /*if (position.Y < 0) // The bird went out of bounds.
                position = new Vector2(position.X, 0);*/

            if (BoundingRectangle.Top < 0) // The character is outside of the bounds)
            {
                position = new Vector2(position.X, 0);
            }
        }

        /// <summary>
        /// Paces back and forth along a platform, waiting at either end.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            // Kill enemy if life <= 0
            if (hp <= 0)
            {
                dead = true;
                return;
            }

            // Update damage timer
            if (damageTime > TimeSpan.Zero)
            {
                damageTime -= gameTime.ElapsedGameTime;

                if (damageTime < TimeSpan.Zero)
                {
                    damageTime = TimeSpan.Zero;
                }
            }

            // Move the bird
            move();
        }

        /// <summary>
        /// Draws the animated enemy.
        /// </summary>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            sprite.PlayAnimation(runAnimation);
            
            // Draw facing the way the enemy is moving.
            SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
            sprite.Draw(gameTime, spriteBatch, Position, flip);
        }
    }
}
